import {cbs, phoneReg, pwdReg} from '../../config/settings.js';
import SendCode from '../Common/SendCode/';
import md5 from 'js-md5';

module.exports = {
    name: 'reminder',
    data() {
        return {
            winSize: {
                width: '',
                height: ''
            },

            formOffset: {
                position: 'absolute',
                left: '',
                top: ''
            },
            // true 为获取验证码，false 为设置新密码
            firstStep: true,

            submit_actions: {
                disabled: false
            },

            data: {
                phone: '',
                code: ''
            },

            code: '',

            phone: '',

            rule_data: {
                phone: [{
                    validator: (rule, value, cb) => {
                        if(this.firstStep) {
                            if(value === '') {
                                cb(new Error('请输入已绑定手机号'));
                            }else if(!phoneReg.test(value)){
                                cb(new Error('请输入有效号码'));
                            }else {
                                cb();
                            }
                        }else {
                            cb();
                        }
                    },
                    trigger: 'blur'
                }],
                code: [{
                    validator: (rule, value, cb) => {
                        if(this.firstStep) {
                            if(value === '') {
                                cb(new Error('请输入验证码'));
                            }else {
                                cb();
                            }
                        }else {
                            cb();
                        }
                    },
                    trigger: 'blur'
                }],
                password: [{
                    validator:(rule, value, callback)=>{
                        if(!this.firstStep) {
                            if (value === '') {
                                callback(new Error('请输入密码'));
                            } else {
                                if(!(/^[a-zA-Z0-9_-]{6,16}$/.test(value))){
                                    callback(new Error('密码至少6位,由大小写字母和数字,-,_组成'));
                                }else{
                                    if(this.register===true){
                                        if (this.data.repassword !== ''){
                                            this.$refs.data.validateField('repassword');
                                        }
                                    }
                                    callback();
                                }
                            }
                        }
                    },
                    trigger: 'blur'
                }]
            },
            userId: ''
        }
    },
    watched: {
    },
    methods: {
        setSize() {
            this.winSize.width = $(window).width() + "px";
            this.winSize.height = $(window).height() + "px";

            this.formOffset.left = (parseInt(this.winSize.width) / 2 - 175) + 'px';
            this.formOffset.top = (parseInt(this.winSize.height) / 2 - 178) + 'px';
        },

        onBackToLogin() {
            this.$router.push('/login');
        },
        handleClick(name) {
            console.log(name)
            if(this.firstStep) {
                this.switchToVlidate();
                this.onValidate(name);
            }else {
                this.switchToModify();
                this.onModify(name);
            }
        },
        onValidate(name) {
            this.$refs[name].validate(valid => {
                if(valid) {
                    let data = this.$$formData({
                        phoneNumber: this.data.phone,
                        verificationCode: this.data.code
                    });

                    this.$$api_user_validate(data, res => {
                        if(res.event === 0) {
                            this.code = this.data.code;
                            this.phone = this.data.phone;
                            this.switchToModify();
                            this.firstStep = false;
                            this.userId = res.obj;
                        }else {
                            cbs.submitError.call(this, `获取验证码失败，${res.msg}`);
                        }
                    })
                }
            })
        },
        onModify(name) {
            this.$refs[name].validate(valid => {
                if(valid) {
                    let pwd = md5(this.data.password);
console.log(this.phone)
                    let data = this.$$formData({
                        phone: this.phone,
                        password: pwd,
                        verificationCode: this.code
                    })

                    this.$$api_user_modifyPwd(data, res => {
                        if(res.event === 0) {
                            this.switchToVlidate();
                            this.firstStep = true;
                            cbs.submitSuccess.call(this, `密码修改成功，请登录`);
                            this.$router.push('/login');
                        }else {
                            cbs.submitError.call(this, `密码修改失败，${res.msg}`);
                        }
                    })
                }
            })
        },
        switchToVlidate() {
            this.$set(this.data, 'phone', '');
            this.$set(this.data, 'code', '');
            this.$delete(this.data, 'password');
        },
        switchToModify() {
            this.$delete(this.data, 'phone');
            this.$delete(this.data, 'code');
            this.$set(this.data, 'password', '');
        },
        handleChange() {
            this.$refs.code.changeStatus(0);
        }
    },
    components: {
        SendCode
    },
    created() {
        this.setSize();
        $(window).resize(() => {
            this.setSize();
        });
        this.switchToVlidate();
    },
    mounted() {
    }
}